//
//  BoxCostHeadView.m
//  XLDoctor
//
//  Created by longma on 2018/10/8.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "MJUICellImageATextView.h"

@implementation MJUICellImageATextView


- (void)awakeFromNib{
    [super  awakeFromNib];
    
    self.leftImageView.hidden = true;
    if(self.iconName){
        self.leftImageView.image = MJUIImageMake(self.iconName);
        self.leftImageView.hidden = false;
    }
    if(self.title){
        self.titleLabel.mj_localizable = self.title;
    }
    if(self.subTitle){
        self.subTitleTextField.text = self.subTitle;
    }
    if(self.hideLine){
        self.horLine.hidden = self.hideLine;
    }
    if(self.hideRightImageView){
        self.rightImageView.hidden = self.hideRightImageView;
    }
    if(self.subTitleColorName){
        self.subTitleTextField.textColor = [UIColor mj_colorName:self.subTitleColorName];
    }
    
    self.backgroundColor = UIColor.clearColor;
    
    if(self.itemType == 1){
        self.leftImageViewW.constant = 30;
        self.leftImageViewH.constant = 30;
    }
    
    self.subTitleTextField.mj_placeholderColor = UIColor.mj_grayColor;
    self.subTitleTextField.adjustsFontSizeToFitWidth = true;
    

}

- (void)setSubTitle:(NSString *)subTitle {
    self.subTitleTextField.hidden = false;
    self.subTitleTextField.text = subTitle;
}

- (void)setSubTitlePlaceholder:(NSString *)subTitlePlaceholder {
    self.subTitleTextField.hidden = false;
    self.subTitleTextField.mj_pLocalizable = subTitlePlaceholder;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    //适配 placeholder 太长显示不全，不会自适应调节字体
    if(self.subTitleTextField.placeholder){
        [self.superview layoutIfNeeded];
        self.subTitleTextField.adjustsFontSizeToFitWidth = true;
        
        UIColor *placeholderColor = UIColor.mj_grayColor;

        NSDictionary<NSAttributedStringKey, id> *placeholderAttributes = @{NSForegroundColorAttributeName: placeholderColor,NSFontAttributeName: self.subTitleTextField.font};
        NSMutableAttributedString *attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:self.subTitleTextField.placeholder attributes:placeholderAttributes];
        
        UITextField *textField = self.subTitleTextField;
        UIFont *placeholderFont = [UIFont systemFontOfSize:textField.font.pointSize];
        
        // 调整占位符字体大小以适应宽度
        CGFloat textWidth = [attributedPlaceholder size].width;
        while (textWidth > textField.bounds.size.width) {
            placeholderFont = [UIFont systemFontOfSize:placeholderFont.pointSize - 1];
    //        NSLog(@"MJLog=======placeholderFont================%lf",placeholderFont.pointSize);
            placeholderAttributes = @{NSFontAttributeName: placeholderFont, NSForegroundColorAttributeName: placeholderColor};
            [attributedPlaceholder setAttributes:placeholderAttributes range:NSMakeRange(0, self.subTitleTextField.placeholder.length)];
            textWidth = [attributedPlaceholder size].width;
        }
        
        //  设置placeholder字体
        self.subTitleTextField.attributedPlaceholder = attributedPlaceholder;
    }
     
}

@end
